package com.gxa.meiye.portal.controller.lzj;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.gxa.meiye.core.common.JsonResult;
import com.gxa.meiye.core.pojo.domain.Order;
import com.gxa.meiye.portal.service.ShopOrderService;
import com.gxa.meiye.portal.service.ShoppayService;
import com.gxa.meiye.portal.utils.PayUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.UUID;


@RestController
@Api(value = "Pay",tags = "支付接口")
@RequestMapping("/shoppay")
public class ShopPayController {

    @Autowired
    private PayUtil payUtil;
    @Autowired
    private ShopOrderService orderService;
    @Autowired
    private ShoppayService payService;
    private static String  orderNumber=null;
    //支付接口
    @ApiOperation(value = "添加到店订单")
    @GetMapping("/paymoney")
    public Map pay(Order order) throws JsonProcessingException {
        String s = UUID.randomUUID().toString().replaceAll("-", "");
        String pay = payUtil.pay(s,order.getGetMoney().toString());
        orderService.update(order.getOrderNumber(),pay);
        JsonResult jsonResult=new JsonResult();
        if (StringUtils.isEmpty(pay)){
            jsonResult.setCode("0");
            jsonResult.setMsg("支付失败");
            return jsonResult.getMap();
        }
        jsonResult.setCode("1");
        jsonResult.setMsg("支付成功");
        return jsonResult.getMap();
    }
    //退款
    @ApiOperation("")
    @GetMapping("/refund")
    public Map refund(String orderNumber,String phone){
        String refund = payService.refund(orderNumber);
        String send = payUtil.send(refund, phone);
        JsonResult jsonResult=new JsonResult();
        if (send.equals("退款成功")){
            //调用后台修改状态
            Integer integer = payService.refundUpdate(orderNumber);
            jsonResult.setCode("1");
            if (integer==1) {

                jsonResult.setMsg("退款成功");
            }else {
                jsonResult.setMsg("状态修改失败");
            }
            return jsonResult.getMap();
        }
        jsonResult.setCode("0");
        jsonResult.setMsg("退款失败");
        return jsonResult.getMap();
    }

}
